package top.glkj.hms.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

import javax.persistence.*;
import java.util.Date;
import java.util.List;

/**
 * @author : xgl
 * @version : 0.0.1
 * @date :2020/12/22 2:18
 */
@Entity
@Table(name="hms_activity")
public class Activity extends BaseEntity {
    /**
     * 活动名称
     */
    @Column(name="active_name")
    private String activeName;
    /**
     * 开始时间
     */
    @Column(name = "start_time")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date startTime;
    /**
     * 结束时间
     */
    @Column(name = "end_time")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date endTime;
    /**
     * 活动描述
     */
    private String description;
    /**
     * 创建人
     */
    @OneToOne(targetEntity = User.class,cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinColumn(name = "insert_user_id", referencedColumnName = "id")
    private User insertUser;

    /**
     * 活动范围
     */
    @Fetch(FetchMode.SUBSELECT)
    @ManyToMany(targetEntity = Room.class,cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinTable(name = "hms_activity_rooms",
            //当前对象在中间表中的外键
            joinColumns = {@JoinColumn(name = "activity_id", referencedColumnName = "id")},
            //对方对象在中间表中的外键
            inverseJoinColumns = {@JoinColumn(name = "room_id", referencedColumnName = "id")}
    )
    private List<Room> roomList;
    /**
     * 活动享有者权限
     */
    @Fetch(FetchMode.SUBSELECT)
    @ManyToMany(targetEntity = Permission.class,cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinTable(name = "hms_activity_permissions",
            //当前对象在中间表中的外键
            joinColumns = {@JoinColumn(name = "activity_id", referencedColumnName = "id")},
            //对方对象在中间表中的外键
            inverseJoinColumns = {@JoinColumn(name = "permission_id", referencedColumnName = "id")}
    )
    private List<Permission> permissionsList;
    /**
     * 活动享有者角色
     */
    @Fetch(FetchMode.SUBSELECT)
    @ManyToMany(targetEntity = Role.class,cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinTable(name = "hms_activity_roles",
            //当前对象在中间表中的外键
            joinColumns = {@JoinColumn(name = "activity_id", referencedColumnName = "id")},
            //对方对象在中间表中的外键
            inverseJoinColumns = {@JoinColumn(name = "role_id", referencedColumnName = "id")}
    )
    private List<Role> roleList;
    /**
     * 活动折扣
     */
    @Column(name = "activity_discount")
    private double activityDiscount;

    /**
     * 活动优惠
     */
    @Column(name = "activity_offers")
    private double activityOffers;

    /**
     * 已参加该活动用户
     */
    @Fetch(FetchMode.SUBSELECT)
    @ManyToMany(targetEntity = User.class,cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinTable(name = "hms_activity_users",
            //当前对象在中间表中的外键
            joinColumns = {@JoinColumn(name = "activity_id", referencedColumnName = "id")},
            //对方对象在中间表中的外键
            inverseJoinColumns = {@JoinColumn(name = "user_id", referencedColumnName = "id")}
    )
    private List<User> userList;
    /**
     * 每一个用户能参加这个活动的次数
     */
    private int count;
}
